﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using DataMongo;
using DataMongo.Entities;
using Google.GData.Client;
using MongoDB.Bson;

using MVCMongo.Services;
using WebMatrix.WebData;

namespace MVCMongo.Controllers
{
    public class CommentController : ApiController
    {    

        public HttpResponseMessage PostPostComment(PostComment postcomment)
        {
         
            postcomment.CommentedBy = WebSecurity.GetUserId(WebSecurity.CurrentUserName);
            postcomment.CommentedDate = DateTime.UtcNow;

                var connection = new RepositoryBaseMongo("PostComment");
                var sources = connection.GetAll<PostComment>().ToList();
                int Id = 0;
                if (sources.Any())
                    Id = sources.Max(x => x.CommentId);
                Id += 1;
                postcomment.CommentId = Id;
                connection.InsertOrUpdate(postcomment);
                var ret = new
                {
                    CommentedBy = postcomment.CommentedBy,
                    CommentedByName = UtilityService.GetCurrentUserFullName(postcomment.CommentedBy),
                    CommentedByAvatar = "/Content/UserProfile/User/" + UtilityService.GetCurrentUserImage(postcomment.CommentedBy),
                    CommentedDate = postcomment.CommentedDate,
                    CommentId = postcomment.CommentId,
                    Message = postcomment.Message,
                    PostId = postcomment.PostId
                };

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, ret);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = postcomment.CommentId }));
                return response;
            }
           
        

        // PUT api/Comment/5
        public HttpResponseMessage PutPostComment(int id, PostComment postcomment)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            if (id != postcomment.CommentId)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }

         
            return Request.CreateResponse(HttpStatusCode.OK);
        }

    }
}